package com.px.project.module.ht.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.px.common.config.security.LoginContextHolder;
import com.px.common.dto.BaseBodyDto;
import com.px.common.dto.BaseQueryDto;
import com.px.common.enums.ApprovePermEnum;
import com.px.common.enums.ApproveStatusEnum;
import com.px.common.enums.PlanOperationStatusEnum;
import com.px.common.util.AssertUtil;
import com.px.common.util.CommonUtil;
import com.px.project.module.ht.dto.DayApproveDto;
import com.px.project.module.ht.dto.DayPlanDto;
import com.px.project.module.ht.dto.DayPlanQueryDto;
import com.px.project.module.ht.entity.DayPlan;
import com.px.project.module.ht.service.DayPlanService;
import com.px.project.module.ht.vo.ApproveStatusVo;
import com.px.project.module.ht.vo.DayPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 后台管理》施工计划》日计划
 *
 * @author 品讯科技
 * @since 2023-10-06
 */
@RestController
@RequestMapping("/dayPlan")
public class DayPlanController {

    @Autowired
    private DayPlanService dayPlanService;

    /**
     * 根据id查询详情
     *
     * @param id id
     * @return {@link DayPlanVo }
     * @author 品讯科技
     * @date 2024-08
     */
    @GetMapping("/get/{id}")
    public DayPlanVo getById(@PathVariable("id") Long id) {
        return this.dayPlanService.getInfoById(id);
    }

    /**
     * 分页查询
     *
     * @param pageVo 页面 VO
     * @param param 参数
     * @return {@link Page }<{@link DayPlanVo }>
     * @author 品讯科技
     * @date 2024-08
     */
    @GetMapping("/page")
    public Page<DayPlanVo> page(@Validated BaseQueryDto<DayPlanVo> pageVo, DayPlanQueryDto param) {
        CommonUtil.setNullValue(param);
        if (CharSequenceUtil.isNotEmpty(param.getApproveStatusCode()) && param.getApproveStatusCode().startsWith("LEVEL_")) {
            param.setNextApproveNode(param.getApproveStatusCode());
            param.setApproveStatusCode(null);
        }
        return this.dayPlanService.selectPageList(pageVo, param);
    }

    /**
     * 保存并提交日计划
     *
     * @param param 参数
     * @return {@link Boolean }
     * @author 品讯科技
     * @date 2024-08
     */
    @PostMapping("/saveAndSubmit")
    @ResponseBody
    public Boolean saveAndSubmit(@RequestBody @Validated DayPlanDto param) {
        this.dayPlanService.saveAndSubmit(param);
        return true;
    }

    /**
     * 更新
     *
     * @param dayPlan 日间计划
     * @return {@link Boolean }
     * @author 品讯科技
     * @date 2024-08
     */
    @PostMapping("/update")
    public Boolean update(@RequestBody DayPlan dayPlan) {
        return this.dayPlanService.updateById(dayPlan);
    }

    /**
     * 删除
     *
     * @param baseBodyDto 请求body基础vo
     * @return {@link Boolean }
     * @author 品讯科技
     * @date 2024-08
     */
    @PostMapping("/delete")
    public Boolean delete(@RequestBody @Validated BaseBodyDto baseBodyDto) {
        return this.dayPlanService.batchDelete(baseBodyDto.getIdList());
    }

    /**
     * 获取周计划审批状态列表
     *
     * @return {@link List }<{@link ApproveStatusVo }>
     * @author 品讯科技
     * @date 2024-08
     */
    @GetMapping("/getApproveStatusList")
    @ResponseBody
    public List<ApproveStatusVo> getWeekStatusList() {
        List<ApproveStatusVo> list = new ArrayList<>();
        list.add(new ApproveStatusVo(ApproveStatusEnum.APPROVED.getCode().toString(), ApproveStatusEnum.APPROVED.getName()));
        list.add(new ApproveStatusVo(ApproveStatusEnum.REJECT.getCode().toString(), ApproveStatusEnum.REJECT.getName()));
        list.add(new ApproveStatusVo(ApprovePermEnum.FIRST.getCode(), "待一级审批"));
        list.add(new ApproveStatusVo(ApprovePermEnum.SECOND.getCode(), "待二级审批"));
        list.add(new ApproveStatusVo(ApprovePermEnum.THIRD.getCode(), "待三级审批"));
        return list;
    }

    /**
     * 流程审核
     *
     * @param param 参数
     * @return {@link Boolean }
     * @author 品讯科技
     * @date 2024-08
     */
    @PostMapping("/approve")
    @ResponseBody
    public Boolean approve(@RequestBody @Validated DayApproveDto param) {
        dayPlanService.approve(param);
        return true;
    }

    /**
     * 计划发起请点
     *
     * @param id id
     * @return {@link Boolean }
     * @author 品讯科技
     * @date 2024-08
     */
    @PostMapping("/click/{id}")
    public Boolean click(@PathVariable("id") Long id) {
        DayPlan plan = dayPlanService.getById(id);
        AssertUtil.checkArgument(ObjectUtil.equal(plan.getApproveStatus(), ApproveStatusEnum.APPROVED.getCode()), "当前计划审批未完成，无法请点");
        AssertUtil.checkArgument(Objects.isNull(plan.getOperationStatus()), "计划已请点，请勿重复操作");
        plan.setOperationStatus(PlanOperationStatusEnum.TO_CLICK.getCode());
        plan.setClickTime(new Date());
        plan.setClickUserId(LoginContextHolder.me().getId());
        return dayPlanService.updateById(plan);
    }

}